home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLINC.PAK / EVENTHAN.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  7KB  |  235 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1992, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.8  $
  6. //
  7. // Definition of TEventHandler and related classes & macros
  8. //----------------------------------------------------------------------------
  9. #if !defined(OWL_EVENTHAN_H)
  10. #define OWL_EVENTHAN_H
  11.  
  12. #if !defined(OWL_SIGNATUR_H)
  13. # include <owl/signatur.h>
  14. #endif
  15. #if !defined(OWL_DISPATCH_H)
  16. # include <owl/dispatch.h>
  17. #endif
  18.  
  19. #if defined(BI_NAMESPACE)
  20. namespace OWL {
  21. #endif
  22.  
  23. // Generic definitions/compiler options (eg. alignment) preceeding the 
  24. // definition of classes
  25. #include <services/preclass.h>
  26.  
  27. //
  28. // Class/operator which converts a Windows message to its string equivalent for
  29. // use with the diagnostic library
  30. //
  31. #if defined(__TRACE) || defined(__WARN)
  32.   class _EXPCLASS ::ostream;
  33.   class _OWLCLASS MsgName {
  34.     public:
  35.       friend ostream& operator <<(ostream& os, const MsgName& msg);
  36.       MsgName(uint msg) : Message(msg) {}
  37.     private:
  38.       uint Message;
  39.   };
  40. #endif
  41.  
  42. //
  43. // Modifier macro used with response tables to put them in far segments if
  44. // needed.
  45. //
  46. #if defined(OWLRTFAR)
  47. # define __RTFAR  __far
  48. #else
  49. # define __RTFAR
  50. #endif
  51.  
  52. //
  53. // Messages defined for OWL use - the top of the user range of ids is reserved
  54. //
  55. #define WM_OWLLAST          0x7FFF
  56. #define WM_OWLFIRST        (WM_OWLLAST - 0x03FF)
  57.  
  58. #define WM_COMMAND_ENABLE  (WM_OWLLAST - 0)
  59. #define WM_CHILDINVALID    (WM_OWLLAST - 1)
  60. #define WM_OWLDOCUMENT     (WM_OWLLAST - 2)
  61. #define WM_OWLVIEW         (WM_OWLLAST - 3)
  62. #define WM_OWLNOTIFY       (WM_OWLLAST - 4)
  63. #define WM_OWLPREPROCMENU  (WM_OWLLAST - 5)
  64. #define WM_OWLCANCLOSE     (WM_OWLLAST - 6)
  65. #define WM_VBXINITFORM     (WM_OWLLAST - 7)
  66. #define WM_VBXNAME         (WM_OWLLAST - 8)
  67. #define WM_VBXBASE         (WM_OWLLAST - 8 - 256)
  68. #define WM_OWLWAKEUP       (WM_VBXBASE - 1)
  69. #define WM_OWLFRAMESIZE    (WM_VBXBASE - 2) // Notify children of frame resizing
  70. #define WM_OWLSLIPDBLCLK   (WM_VBXBASE - 3) // Notify parent of user dblclick of edge slip
  71. #define WM_OWLWINDOWDOCKED (WM_VBXBASE - 4) // Notify window it was [un]docked/reparented
  72. #define WM_OWLCREATETTIP   (WM_VBXBASE - 5) // Notify gadget window to create tooltips
  73.  
  74. //
  75. // Portable types for message & event components
  76. //
  77. typedef LRESULT TResult;  // Returned result of a message
  78. typedef uint    TMsgId;   // Message ID type 
  79. typedef WPARAM  TParam1;  // Type of first message parameter (uint)
  80. typedef LPARAM  TParam2;  // Type of second message parameter (uint32)
  81.  
  82. template <class T1, class T2>
  83. inline TParam2 MkParam2(const T1& lo, const T2& hi) {
  84.   return (uint32(hi) << 16) | uint16(lo);
  85. }
  86. #if !defined(BI_PLAT_WIN16)
  87. inline TParam1 MkParam1(uint lo, uint hi) {
  88.   return (uint32(hi) << 16) | uint16(lo);
  89. }
  90. #endif
  91.  
  92. struct TMsg {  //  : public tagMSG
  93.   HWND    HWnd;
  94.   uint    Message;
  95.   TParam1 Param1;
  96.   TParam2 Param2;
  97.   uint32  Time;
  98.   TPoint  Pt;
  99.  
  100.   operator MSG&() {return *this;}
  101. };
  102.  
  103. //
  104. // Forward declarations
  105. //
  106. template <class T> class TResponseTableEntry;
  107. typedef TResponseTableEntry<GENERIC>  TGenericTableEntry;
  108.  
  109. //
  110. // class TEventHandler
  111. // ~~~~~ ~~~~~~~~~~~~~
  112. // Base class from which to derive classes that can handle events
  113. //
  114. class _OWLCLASS _RTTI TEventHandler {
  115.   public:
  116.     class TEventInfo {
  117.       public:
  118.         const uint                  Msg;
  119.         const uint                  Id;
  120.         GENERIC*                    Object;
  121.         TGenericTableEntry __RTFAR* Entry;
  122.  
  123.         TEventInfo(uint msg, uint id=0) : Msg(msg), Id(id) {Entry = 0;}
  124.     };
  125.     typedef bool(*TEqualOperator)(TGenericTableEntry __RTFAR&, TEventInfo&);
  126.  
  127.     // Searches the list of response table entries looking for a match
  128.     //
  129.     virtual bool     Find(TEventInfo& info, TEqualOperator op = 0);
  130.  
  131.     TResult          Dispatch(TEventInfo& info, TParam1 p1, TParam2 p2 = 0);
  132.     TResult          DispatchMsg(uint msg, uint id, TParam1 p1, TParam2 p2);
  133.  
  134.   protected:
  135.     bool             SearchEntries(TGenericTableEntry __RTFAR* entries,
  136.                                    TEventInfo& info,
  137.                                    TEqualOperator op);
  138. };
  139.  
  140. //
  141. // class TResponseTableEntry<>
  142. // ~~~~~ ~~~~~~~~~~~~~~~~~~~~~
  143. // Entry in a response table
  144. //
  145. template <class T> class TResponseTableEntry {
  146.   public:
  147.     typedef void (T::*PMF)();
  148.  
  149.     union {
  150.       uint          Msg;
  151.       uint          NotifyCode;
  152.     };
  153.     uint            Id;
  154.     TAnyDispatcher  Dispatcher;
  155.     PMF             Pmf;
  156. };
  157.  
  158. // Generic definitions/compiler options (eg. alignment) following the 
  159. // definition of classes
  160. #include <services/posclass.h>
  161.  
  162. #if defined(BI_NAMESPACE)
  163. } // namespace OWL
  164. #endif
  165.  
  166. //
  167. // Macros to declare a response table
  168. //
  169. #define DECLARE_RESPONSE_TABLE(cls)\
  170.   private:\
  171.     static TResponseTableEntry< cls > __RTFAR __entries[];\
  172.     typedef TResponseTableEntry< cls >::PMF   TMyPMF;\
  173.     typedef cls                               TMyClass;\
  174.   public:\
  175.     bool  Find(TEventInfo&, TEqualOperator = 0)
  176.  
  177. #define END_RESPONSE_TABLE\
  178.   {0, 0, 0, 0}}
  179.  
  180. #define DEFINE_RESPONSE_TABLE_ENTRIES(cls)\
  181.   TResponseTableEntry< cls > __RTFAR  cls::__entries[] = {
  182.  
  183. //
  184. // Macro to define a response table for a class with no base response tables
  185. //
  186. // Use it like this:
  187. //    DEFINE_RESPONSE_TABLE(cls)
  188. //      EV_WM_PAINT,
  189. //      EV_WM_LBUTTONDOWN,
  190. //    END_RESPONSE_TABLE;
  191. //
  192. #define DEFINE_RESPONSE_TABLE(cls)\
  193.   bool  cls::Find(TEventInfo& eventInfo, TEqualOperator equal)\
  194.       {eventInfo.Object = (GENERIC*)this;\
  195.        return SearchEntries((TGenericTableEntry __RTFAR*)__entries, eventInfo, equal);}\
  196.   DEFINE_RESPONSE_TABLE_ENTRIES(cls)
  197.  
  198. //
  199. // Macro to define a response table for a class with one base. Use this macro
  200. // exactly like macro DEFINE_RESPONSE_TABLE
  201. //
  202. #define DEFINE_RESPONSE_TABLE1(cls, base)\
  203.   bool  cls::Find(TEventInfo& eventInfo, TEqualOperator equal)\
  204.       {eventInfo.Object = (GENERIC*)this;\
  205.        return SearchEntries((TGenericTableEntry __RTFAR*)__entries, eventInfo, equal) ||\
  206.               base::Find(eventInfo, equal);}\
  207.   DEFINE_RESPONSE_TABLE_ENTRIES(cls)
  208.  
  209. //
  210. // Macro to define a response table for a class with two bases. Use this macro
  211. // exactly like macro DEFINE_RESPONSE_TABLE
  212. //
  213. #define DEFINE_RESPONSE_TABLE2(cls, base1, base2)\
  214.   bool  cls::Find(TEventInfo& eventInfo, TEqualOperator equal)\
  215.       {eventInfo.Object = (GENERIC*)this;\
  216.        return SearchEntries((TGenericTableEntry __RTFAR*)__entries, eventInfo, equal) ||\
  217.               base1::Find(eventInfo, equal) ||\
  218.               base2::Find(eventInfo, equal);}\
  219.   DEFINE_RESPONSE_TABLE_ENTRIES(cls)
  220.  
  221. //
  222. // Macro to define a response table for a class with three bases. Use this macro
  223. // exactly like macro DEFINE_RESPONSE_TABLE
  224. //
  225. #define DEFINE_RESPONSE_TABLE3(cls, base1, base2, base3)\
  226.   bool  cls::Find(TEventInfo& eventInfo, TEqualOperator equal)\
  227.       {eventInfo.Object = (GENERIC*)this;\
  228.        return SearchEntries((TGenericTableEntry __RTFAR*)__entries, eventInfo, equal) ||\
  229.               base1::Find(eventInfo, equal) ||\
  230.               base2::Find(eventInfo, equal) ||\
  231.               base3::Find(eventInfo, equal);}\
  232.   DEFINE_RESPONSE_TABLE_ENTRIES(cls)
  233.  
  234. #endif  // OWL_EVENTHAN_H
  235.